﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{

    public partial class AdminAddNews : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            else
            {
                GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
                OnlineUser ou = (OnlineUser)Session["User"];
                if (ou.UserAccessLevel != ("5"))
                {
                    Response.Redirect("~/Default.aspx");
                }

                
                if (ou.TeamId == "Null")
                    Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");
            }

            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.AdminAddNews_PageTitle;

        }


        protected String AddNewsToDB(string Title, string Body, string Language, string UserId, string Nationality, bool NeedsTranslation)
        {
            string returnValue = "error";
            string conn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            System.Data.SqlClient.SqlConnection
            strConnection = new System.Data.SqlClient.SqlConnection(conn);
            string StrSQL = "Nivdal_AddNewsItem";
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(StrSQL, strConnection);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Title", Title));
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Body", Body));
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Language", Language));
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserId", UserId));
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Nationality", Nationality));
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@NeedsTranslation", NeedsTranslation ? "1" : "0"));
            Response.Write(Title + " " + Body + " " + Language + " " + UserId + " " + Nationality);
            strConnection.Open();
            try
            {
                int id = Convert.ToInt32(cmd.ExecuteScalar());
                //MessageLabel.Text = "The news was added!";
                Response.Redirect("AdminAddNewsEndsuccessfully.aspx?MessageString=" + Resources.Resource.AdminAddNews_NewsAddedSuccessfully);
            }
            catch (Exception SQLDBException)
            {
                MessageLabel.Text = Convert.ToString(SQLDBException);
                //return;

            }

            finally
            {
                strConnection.Close();
            }
            return returnValue;

        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            bool IsNewsGood = false;
            //GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();

            String Title = txtTName.Text.ToString();
            //Check if threre are minimum ammount of chars in the news
            if (NewsTextArea.Text.Length > 10)
                IsNewsGood = true;
            else
                MessageLabel.Text = Resources.Resource.AdminAddNews_NewsTextArea;

            string Language = LanguageDropDownList.SelectedValue.ToString();
            string Nationality = NationalityDropDownList.SelectedValue.ToString();
            bool NeedsTranslation = ((ou.UserAccessLevel != "5") || (TranslationNeededCheckBox.Checked));
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                IsNewsGood = false;
            //Response.Write(UserId);
            if (IsNewsGood)
            {
                //Response.Write(UserId);
                AddNewsToDB(Title, NewsTextArea.Text.Trim(), Language, ou.UserId, Nationality, NeedsTranslation);
            }
            else
                MessageLabel.Text = Resources.Resource.AdminAddNews_NoNewsGood;

        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        protected void ApproveItem(object sender, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Approve")
            {
                GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
                MessageLabel.Text = GND.ReadNivdalStringData("NewsItemId", "Nivdal_ApproveNewsItemafterTranslation", e.CommandArgument);
            }
        }


    }
}